package com.wwwzh.himalayafm.fragment.disfragment.recommend;


import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.wwwzh.himalayafm.BR;
import com.wwwzh.himalayafm.R;
import com.wwwzh.himalayafm.activity.XmlyActivity;
import com.wwwzh.himalayafm.adapter.recommend.AlbumDetailPagerAdapter;
import com.wwwzh.himalayafm.adapter.recommend.AlbumShowAdapter;
import com.wwwzh.himalayafm.entity.recommend.albumdetailshow.AlbumDetailShow;
import com.wwwzh.himalayafm.entity.recommend.albumdetailshow.AlbumDetailShowAlbum;
import com.wwwzh.himalayafm.fragment.subfragment.MyItemDecorations;
import com.wwwzh.himalayafm.retrofit.HttpService;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 */
public class AlbumDetailFragment extends Fragment implements Callback<AlbumDetailShow> {
    private String albumId = "376177";

    private Context context;
    private AppCompatActivity activity;
    private List<View> views;
    private RecyclerView recyclerView;
    private ViewPager viewpager;

    private AlbumDetailShowAlbum album;
    private ViewDataBinding binding;
    private LinearLayout albumFragLayout;
    private Animation animation;
    private LinearLayout layoutForGone;

    private RelativeLayout animLayout;

    public AlbumDetailFragment() {
        // Required empty public constructor
    }


    public static AlbumDetailFragment newInstance(String albumId) {
        Bundle args = new Bundle();
        args.putString("albumId",albumId);
        AlbumDetailFragment fragment = new AlbumDetailFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        albumId = getArguments().getString("albumId");
        context = getContext();
        activity = (AppCompatActivity) getActivity();
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        binding = DataBindingUtil.inflate(LayoutInflater.from(context),R.layout.layout_album_detail_top,null,false);

        View rootView = inflater.inflate(R.layout.fragment_album_detail, container, false);
        albumFragLayout = (LinearLayout) rootView.findViewById(R.id.albumDetail_ll);
        layoutForGone = (LinearLayout) rootView.findViewById(R.id.albumLayout_forGone);
        FrameLayout topFramelayout = (FrameLayout) rootView.findViewById(R.id.topFramelayout);
        topFramelayout.addView(binding.getRoot());

        Toolbar toolbar = (Toolbar) rootView.findViewById(R.id.AlbumDTopTit_Tl);

        activity.setSupportActionBar(toolbar);
        ActionBar supportActionBar = activity.getSupportActionBar();
        supportActionBar.setDisplayHomeAsUpEnabled(true);
        supportActionBar.setTitle("专辑详情");
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                XmlyActivity activity = (XmlyActivity) getActivity();
                activity.getDrawerLayout().closeDrawers();
            }
        });

        viewpager = (ViewPager) rootView.findViewById(R.id.AlbumDeTopSta_VP);
        TabLayout tabLayout = (TabLayout) rootView.findViewById(R.id.AlbumDeTopSta_TL);
        tabLayout.setupWithViewPager(viewpager);

        View detailView = LayoutInflater.from(context).inflate(R.layout.layout_album_detail,null);
        View detailView1 = LayoutInflater.from(context).inflate(R.layout.layout_album_show, null);

        recyclerView = (RecyclerView) detailView1.findViewById(R.id.AlbumDeShow_RV);

        views = new ArrayList<>();
        views.add(detailView);
        views.add(detailView1);

        AlbumDetailPagerAdapter albumDetailPagerAdapter = new AlbumDetailPagerAdapter(views);
        viewpager.setAdapter(albumDetailPagerAdapter);
        return rootView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        layoutForGone.setVisibility(View.GONE);
        addLoadAnim();
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://mobile.ximalaya.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        HttpService albumservice = retrofit.create(HttpService.class);
        albumservice.getAlbumShow().enqueue(this);
    }

    //加载动画
    private void addLoadAnim() {
        View viewLoad = LayoutInflater.from(getContext()).inflate(R.layout.layout_loading, albumFragLayout, false);
        animLayout = (RelativeLayout) viewLoad.findViewById(R.id.LoadingAnim_RL);
        ImageView imageView = (ImageView) viewLoad.findViewById(R.id.LoadingAnim_IV);
        animation = AnimationUtils.loadAnimation(getContext(), R.anim.anim_load);
        LinearInterpolator lin = new LinearInterpolator();
        animation.setInterpolator(lin);
        imageView.startAnimation(animation);
        albumFragLayout.addView(animLayout);
    }


    @Override
    public void onResponse(Call<AlbumDetailShow> call, Response<AlbumDetailShow> response) {
        if (response.body() !=null){
            album = response.body().getData().getAlbum();
            binding.setVariable(BR.albumContent,album);
//            initTopView(album);
            AlbumShowAdapter adapter = new AlbumShowAdapter(context,response.body().getData().getTracks().getList());
            recyclerView.setAdapter(adapter);
            MyItemDecorations itemDecorations = new MyItemDecorations(5);
            recyclerView.addItemDecoration(itemDecorations);
            removeLoadAnim();
        }

    }

    // 当数据下载完成移除动画
    private void removeLoadAnim() {
        animation.cancel();
        albumFragLayout.removeView(animLayout);
        layoutForGone.setVisibility(View.VISIBLE);
    }

    private void initTopView(AlbumDetailShowAlbum album) {
        ViewDataBinding binding = DataBindingUtil.inflate(LayoutInflater.from(context),R.layout.layout_album_detail_top,null,false);
        binding.setVariable(BR.albumContent,album);
    }

    @Override
    public void onFailure(Call<AlbumDetailShow> call, Throwable t) {

    }
}
